###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/ModifyStyleSheet.pm.pl -> ModifyStyleSheet module       #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::ModifyStyleSheet;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use HTML::Form;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, error => "", @_);

  my @Sections = (
    { title => "Body Properties", items => [
      { name => "BODY_PADDING", subject => "Padding", type => "textbox", required => 1 },
      { name => "BODY_IMAGE", subject => "Background Image", type => "textbox" },
      { name => "BODY_BGCOLOR", subject => "Background Color", type => "color", required => 1 }
    ]},
    { title => "Font Properties", items => [
      { name => "FONT_COLOR", subject => "Color", type => "color", required => 1 },
      { name => "FONT_FACE", subject => "Face", type => "textbox", required => 1 },
      { name => "FONT_SIZE", subject => "Size", type => "size", required => 1 },
      { name => "FONT_ERROR", subject => "Error Color", type => "color", required => 1 }
    ]},
    { title => "Link Properties", items => [
      { name => "LINK_COLOR", subject => "Color", type => "color", required => 1 },
      { name => "LINK_VCOLOR", subject => "Visited Color", type => "color", required => 1 },
      { name => "LINK_HCOLOR", subject => "Hover Color", type => "color", required => 1 },
      { name => "LINK_ACOLOR", subject => "Active Color", type => "color", required => 1 }
    ]},
    { title => "Main Table Properties", help => "MainTable.jpg", items => [
      { name => "MAIN_WIDTH", subject => "Width", type => "textbox", required => 1 },
      { name => "MAIN_BORDER", subject => "Border Color", type => "color", required => 1 },
      { name => "MAIN_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "MAIN_ROWBORDER", subject => "Row Border Color", type => "color", required => 1 }
    ]},
    { title => "Title Properties", help => "Title.jpg", items => [
      { name => "TITLE_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "TITLE_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "TITLE_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},
    { title => "Menu Properties", help => "Menu.jpg", items => [
      { name => "MENU_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "MENU_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "MENU_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},
    { title => "Location Properties", help => "Location.jpg", items => [
      { name => "LOCATION_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "LOCATION_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},    
    { title => "Main Header Properties", help => "MainHeader.jpg", items => [
      { name => "HEADER_IMAGE", subject => "Background Image", type => "textbox" },
      { name => "HEADER_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "HEADER_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "HEADER_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},
    { title => "Main Dialog Properties", help => "MainDialog.jpg", items => [
      { name => "DIALOG_BGCOLOR", subject => "Background Color", type => "color", required => 1 }
    ]},
    { title => "Inner Table Properties", help => "InnerTable.jpg", items => [
      { name => "INNER_BORDER", subject => "Border Color", type => "color", required => 1 },
      { name => "INNER_BGCOLOR", subject => "Background Color", type => "color", required => 1 }
    ]},
    { title => "Inner Header Properties", help => "InnerHeader.jpg", items => [
      { name => "IHEADER_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "IHEADER_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "IHEADER_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},
    { title => "Row Properties", help => "Row.jpg", items => [
      { name => "ROW_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "ROW_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "ROW_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},
    { title => "Footer Properties", help => "Footer.jpg", items => [
      { name => "FOOTER_BGCOLOR", subject => "Background Color", type => "color", required => 1 },
      { name => "FOOTER_COLOR", subject => "Font Color", type => "color", required => 1 },
      { name => "FOOTER_SIZE", subject => "Font Size", type => "size", required => 1 }
    ]},
    { title => "Bottom Bar Properties", help => "BottomBar.jpg", items => [
      { name => "BOTTOMBAR_HEIGHT", subject => "Height", type => "textbox", required => 1 },
      { name => "BOTTOMBAR_BGCOLOR", subject => "Background Color", type => "color", required => 1 }
    ]},
    { title => "Divider Properties", help => "Divider.jpg", items => [
      { name => "DIVIDER_BGCOLOR1", subject => "First Color", type => "color", required => 1 },
      { name => "DIVIDER_BGCOLOR2", subject => "Second Color", type => "color", required => 1 }
    ]},
    { title => "Form Properties", help => "Form.jpg", items => [
      { name => "REQUIRED_COLOR", subject => "Required Star Color", type => "color", required => 1 },
      { name => "REQUIRED_SIZE", subject => "Required Star Size", type => "size", required => 1 },
      { name => "SUBJECT_COLOR", subject => "Subject Font Color", type => "color", required => 1 },
      { name => "SUBJECT_SIZE", subject => "Subject Font Size", type => "size", required => 1 },
      { name => "FIELD_COLOR", subject => "Field Font Color", type => "color", required => 1 },
      { name => "FIELD_SIZE", subject => "Field Font Size", type => "size", required => 1 },
      { name => "LABEL_COLOR", subject => "Label Font Color", type => "color", required => 1 },
      { name => "LABEL_SIZE", subject => "Label Font Size", type => "size", required => 1 },
      { name => "FORM_FACE", subject => "Input Field Font Face", type => "textbox", required => 1 },
      { name => "FORM_BGCOLOR", subject => "Input Field Background Color", type => "color" },
      { name => "FORM_COLOR", subject => "Input Field Font Color", type => "color", required => 1 },
      { name => "FORM_SIZE", subject => "Input Field Font Size", type => "size", required => 1 },
      { name => "FORM_WIDTH", subject => "Input Field Width", type => "textbox", required => 1 },
      { type => "divider" },
      { type => "buttons" }
    ]}
  );

  my $LANGUAGE = {
    "MISSING" => {
      "DESCRIPTION"   => qq~<li>You didn't fill in the "Description" field.~
    },
    "TOOLONG" => {
      "DESCRIPTION"   => qq~<li>The value for the "Description" field must be 256 characters or less.~
    },
    "ERROR"         => qq~<p><font class="error">There were errors:<ul>[%error%]</ul></font>~
  };

  foreach my $section (@Sections) {
    foreach my $item (@{ $section->{'items'} }) {
      $LANGUAGE->{'MISSING'}->{ $item->{'name'} } = qq~<li>You didn't fill in the "$item->{'subject'}" field.~ if ($item->{'subject'});
    }
  }

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();
  my $Form   = HTML::Form->new();

  my $Body = $Dialog->TableRow(items => [{ body => "Please fill out all the required (<font class=\"form-required\"><b>*</b></font>) fields:".$in{'error'}, colspan => "2" }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'STYLESHEET'}->{'ID'},
    subject   => "ID"
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_DESCRIPTION",
    value     => $in{'input'}->{'STYLESHEET'}->{'DESCRIPTION'},
    subject   => "Description",
    required  => 1
  );

  foreach my $section (@Sections) {
    if ($section->{'help'} && $section->{'title'}) {
      my $title  = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr>";
         $title .= "<td width=\"100%\"><font class=\"table-header\">$section->{'title'}</font></td>";
         $title .= "<td width=\"1\" nowrap><a href=\"javascript:OpenStyleHelp('$SYSTEM->{'PUBLIC_URL'}/Images/Styles/$section->{'help'}');\"><img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/iconhelp.gif\" alt=\"Show context\"></a></td>";
         $title .= "</tr></table>";
      $Body .= $Dialog->TableHeader(titles => [{ title => $title, colspan => 2, align => "left" }]);
    } elsif ($section->{'title'}) {
      $Body .= $Dialog->TableHeader(titles => [{ title => $section->{'title'}, colspan => 2, align => "left" }]);
    }

    foreach my $item (@{ $section->{'items'} }) {
      if ($item->{'type'} eq "textbox") {
        $Body .= $Dialog->FormTextBox(
          name      => "FORM_".$item->{'name'},
          value     => $in{'input'}->{'STYLESHEET'}->{'stylesheet'}->{$item->{'name'}},
          subject   => $item->{'subject'},
          required  => $item->{'required'}
        );
      } elsif ($item->{'type'} eq "color") {
        my $value = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td>";
        $value .= $Form->TextBox(
          name      => "FORM_".$item->{'name'},
          value     => $in{'input'}->{'STYLESHEET'}->{'stylesheet'}->{$item->{'name'}},
          extra     => { onBlur => "UpdateColorPicker(document.all.CELL_$item->{'name'}, this);" }
        );
        $value .= "</td><td width=\"25\"><table cellpadding=\"0\" cellspacing=\"1\" border=\"0\" bgcolor=\"#000000\" width=\"100%\"><tr>";
        $value .= "<td id=\"CELL_$item->{'name'}\" bgcolor=\"".($SL::QUERY{'FORM_'.$item->{'name'}} || $in{'input'}->{'STYLESHEET'}->{'stylesheet'}->{$item->{'name'}})."\"><a href=\"#\" onClick=\"OpenColorPicker('CELL_$item->{'name'}', 'form', 'FORM_$item->{'name'}', '$SYSTEM->{'PUBLIC_URL'}')\"><img src=\"$SYSTEM->{'PUBLIC_URL'}/Images/blank.gif\" width=\"25\" height=\"17\" border=\"0\" alt=\"Color Picker\"></a></td></tr></table>";
        $value .= "</td></tr></table>";
        $Body .= $Dialog->FormTextBox(
          value     => $value,
          subject   => $item->{'subject'},
          required  => $item->{'required'}
        );
      } elsif ($item->{'type'} eq "size") {
        $Body .= $Dialog->FormSelectBox(
          name      => "FORM_".$item->{'name'},
          value     => $in{'input'}->{'STYLESHEET'}->{'stylesheet'}->{$item->{'name'}},
          subject   => $item->{'subject'},
          required  => $item->{'required'},
          items     => [
            { value => "10px", label => "10px" },
            { value => "11px", label => "11px" },
            { value => "12px", label => "12px" },
            { value => "13px", label => "13px" },
            { value => "14px", label => "14px" },
            { value => "15px", label => "15px" },
            { value => "16px", label => "16px" },
            { value => "17px", label => "17px" },
            { value => "18px", label => "18px" },
            { value => "19px", label => "19px" },
            { value => "20px", label => "20px" }
          ]
        );
      } elsif ($item->{'type'} eq "divider") {
        $Body .= $Dialog->TableDivider(colspan => 2);
      } elsif ($item->{'type'} eq "buttons") {
        $Body .= $Dialog->FormButton(
          items => [
            { type => "submit", value => "Modify" },
            { type => "reset", value => "Cancel" }
          ], join => "&nbsp;"
        );
      }
    }
  }

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body        => $Body,
    header      => "Modify a StyleSheet",
    head_extra  => "<script src=\"$SYSTEM->{'PUBLIC_URL'}/functions.js\"></script>",
    form        => 1,
    hiddens     => [
      { name => "Referer", value => $SL::QUERY{'Referer'} },
      { name => "SSID", value => $in{'input'}->{'STYLESHEET'}->{'ID'} },
      { name => "action", value => "DoModifyStyleSheet" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ],
    (!$in{'input'}->{'STYLESHEET'}->{'LOCKED'} ? (toolbar => [
      { type => "divider" },
      { type => "icon", url => "javascript:ConfirmRemove('$SYSTEM->{'SCRIPT_URL'}?CP=1&action=DoRemoveStyleSheets&SSID=$in{'input'}->{'STYLESHEET'}->{'ID'}&Referer=".$SL::CGI->escape($SL::CGI->escape($SL::QUERY{'Referer'}))."&SID=$SL::ADMIN{'SID'}');", icon => "Toolbar/remove.gif", caption => "Remove" }
    ]) : ())
  );
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "The following stylesheet has been modified:", colspan => 2 }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'STYLESHEET'}->{'ID'},
    subject   => "ID"
  );
  
  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'DESCRIPTION'},
    subject   => "Description"
  );

  $Body .= $Dialog->TableDivider(colspan => 2);
  $Body .= $Dialog->TableRow(items => [{ body => "Click <a href=\"".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=StyleSheets&SID=$SL::ADMIN{'SID'}")."\">here</a> if you're not redirected in 5 seconds.", colspan => 2 }]);

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Modify a StyleSheet",
    metas   => [
      { http_equiv => "refresh", content => "5; url=".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=StyleSheets&SID=$SL::ADMIN{'SID'}") }
    ]
  );
  
  return $Body;
}

1;